EnablePrimaryMouseButtonEvents(true)

-- 强化版键位映射表（基于罗技官方扫描码）
local key_conversion = {
    ["-"] = "minus",    -- 标准减号
    ["="] = "equals",   -- 等号键（基础物理键）
    ["+"] = {"lshift", "equals"}  -- 加号需要组合键实现
}

-- 组合键列表（区分基础键和组合符号）
local ctrl_keys = {"3", "4", "5", "6", "7", "8", "9", "0", "-"}
local shift_keys = {"3", "4", "5", "6", "7", "8", "9", "0", "-"}

-- 索引控制
local ctrl_index = 1
local shift_index = 1

function OnEvent(event, arg)
    -- G4连发控制（Ctrl系列）
    if IsMouseButtonPressed(4) then
        OutputLogMessage("===== G4触发Ctrl组合循环 =====\n")
        PressKey("lctrl")
        
        repeat
            local original_key = ctrl_keys[ctrl_index]
            OutputLogMessage("[预处理] 原始键值：%s\n", original_key)
            
            -- 分层处理特殊键
            if type(key_conversion[original_key]) == "table" then
                -- 组合键特殊处理（如+号）
                local modifier = key_conversion[original_key][1]
                local base_key = key_conversion[original_key][2]
                
                OutputLogMessage("[复合键] 检测到组合键：%s + %s\n", modifier, base_key)
                PressKey(modifier)
                PressKey(base_key)
                Sleep(5)
                ReleaseKey(base_key)
                ReleaseKey(modifier)
            else
                -- 单键处理
                local converted_key = key_conversion[original_key] or original_key
                PressKey(converted_key)
                Sleep(5)
                ReleaseKey(converted_key)
            end
            
            -- 安全循环索引
            ctrl_index = (ctrl_index % #ctrl_keys) + 1
            OutputLogMessage("[索引更新] CTRL序列：%d/%d\n", ctrl_index, #ctrl_keys)
            Sleep(5)
        until not IsMouseButtonPressed(4)
        
        ReleaseKey("lctrl")
        OutputLogMessage("===== G4释放结束循环 =====\n\n")
    
    -- G5连发控制（Shift系列）
    elseif IsMouseButtonPressed(5) then
        OutputLogMessage("===== G5触发Shift组合循环 =====\n")
        PressKey("lshift")
        
        repeat
            local original_key = shift_keys[shift_index]
            OutputLogMessage("[预处理] 原始键值：%s\n", original_key)
            
            if type(key_conversion[original_key]) == "table" then
                local modifier = key_conversion[original_key][1]
                local base_key = key_conversion[original_key][2]
                
                OutputLogMessage("[复合键] 检测到组合键：%s + %s\n", modifier, base_key)
                PressKey(modifier)
                PressKey(base_key)
                Sleep(5)
                ReleaseKey(base_key)
                ReleaseKey(modifier)
            else
                local converted_key = key_conversion[original_key] or original_key
                PressKey(converted_key)
                Sleep(5)
                ReleaseKey(converted_key)
            end
            
            shift_index = (shift_index % #shift_keys) + 1
            OutputLogMessage("[索引更新] SHIFT序列：%d/%d\n", shift_index, #shift_keys)
            Sleep(5)
        until not IsMouseButtonPressed(5)
        
        ReleaseKey("lshift")
        OutputLogMessage("===== G5释放结束循环 =====\n\n")
    end
end